package upm.pb4agent.team5.custom;

import jade.domain.FIPAException;
import jade.util.leap.ArrayList;
import jade.util.leap.List;
import upm.pb4agent.ontology.Dock;
import upm.pb4agent.team5.BaseAgent;
import upm.pb4agent.team5.common.Watcher;
import upm.pb4agent.team5.util.ConfigFile;
import upm.pb4agent.team5.util.RowerWrapper;

public class Rower extends BaseAgent {
	private static final long serialVersionUID = -3827531657185393334L;
	public static final String ACTIVATE_PROTOCOL = "Activate";
	public static final String CHANGE_SEAT_PROTOCOL = "ChangeSeat";
	public static final String SEND_KI_PROTOCOL = "SendKi";

	public final static String[] DF_NAME = { "Rower" + Manager.TEAM_ID + "1",
			"Rower" + Manager.TEAM_ID + "2", "Rower" + Manager.TEAM_ID + "3",
			"Rower" + Manager.TEAM_ID + "4" };

	private upm.pb4agent.ontology.Boat boat = null;
	private List docks = null;
	private boolean inBoat = false;
	private boolean rowing = false;
	private java.util.List<RowerWrapper> rowersWrapper = null;
	private String seat = null;

	private float strength = 0F;
	private float resistance = 0F;
	private float agility = 0F;
	private int numberOfRows = 0;

	public Rower() {
		super(null); // Setting DF null by default
		docks = new ArrayList();
		Dock dock = new Dock();
		dock.setName(Watcher.DOCK_NAME[0]);
		docks.add(dock);

	}

	protected void setup() {
		setDfName(getLocalName()); // For register agent in DF with its
									// localName as DF_NAME

		// Calculating properties for Ki
		char rowerId = getDfName().charAt(getDfName().length() - 1);
		strength = Float.parseFloat(ConfigFile.getProperty("rower" + rowerId + "_strength"));
		resistance = Float.parseFloat(ConfigFile.getProperty("rower" + rowerId + "_resistance"));
		agility = Float.parseFloat(ConfigFile.getProperty("rower" + rowerId + "_agility"));

		System.out.println(getLocalName() + ": Enters the system");

		try {
			registerAgent(); // Registering in DF
			System.out.println(getLocalName() + ": Registered in DF");

			addBehaviour(new BRequestRefereeSubscription(this));
			addBehaviour(new BRowerManageMessages(this));

		} catch (FIPAException e) {
			System.err.println(getLocalName() + ": Cannot register in DF");
			doDelete(); // Terminating agent
		}

	}

	public upm.pb4agent.ontology.Boat getBoat() {
		return boat;
	}

	public void setBoat(upm.pb4agent.ontology.Boat boat) {
		this.boat = boat;
	}

	public List getDocks() {
		return docks;
	}

	public float getKi() {
		return (agility + resistance + strength) / (3* (1 + numberOfRows));
	}

	public void incrementNumberOfRows() {
		this.numberOfRows++;
	}

	public static float getStartingKi(String name) {
		char rowerId = name.charAt(name.length() - 1);
		float strength = Float.parseFloat(ConfigFile.getProperty("rower" + rowerId + "_strength"));
		float resistance = Float.parseFloat(ConfigFile.getProperty("rower" + rowerId + "_resistance"));
		float agility = Float.parseFloat(ConfigFile.getProperty("rower" + rowerId + "_agility"));

		return (agility + resistance + strength) / 3;
	}

	public boolean isInBoat() {
		return inBoat;
	}

	public void setInBoat(boolean inBoat) {
		this.inBoat = inBoat;
	}

	public boolean isRowing() {
		return rowing;
	}

	public void setRowing(boolean rowing) {
		this.rowing = rowing;
	}

	public java.util.List<RowerWrapper> getRowersWrapper() {
		return rowersWrapper;
	}

	public String getSeat() {
		return seat;
	}

	public void setSeat(String seat) {
		this.seat = seat;
	}

	public void setRowersWrapper(java.util.List<RowerWrapper> rowersWrapper) {
		this.rowersWrapper = rowersWrapper;
	}

}
